#include<iostream>
#include<cmath>
#include<string>
using namespace std;
string s;
int main()
{
	int t;
	cin >> t;
	while (t--) {
		int n, x1, y1, x2, y2;
		cin >> n >> x1 >> y1 >> x2 >> y2;
		int ans1 = abs(n - y1), ans2 = abs(n - y2);
		if (y1 * 2<= n) {
			ans1 = abs(y1 - 1);
		}
		if (y2 * 2<= n) {
			ans2 = abs(y2 - 1);
		}
		ans1 = min(min(abs(x1 - 1), abs(n - x1)), ans1);
		ans2 = min(min(abs(x2 - 1), abs(n - x2)), ans2);
		cout << abs(ans1 - ans2) << endl;
	}
	return 0;
}